import type { VxeTableGridOptions } from '@vben/plugins/vxe-table';

import type { OnActionClickFn } from '#/adapter/vxe-table';
import type { ArrivalNotice, DictApi, ItemRecptLine } from '#/api';

import { reactive } from 'vue';

import { DictResp } from '#/api';

const status = reactive<DictApi.SelectOption[]>([]);

DictResp(['mes_order_status']).then((res) => {
  if (res.mes_order_status) {
    status.push(...res.mes_order_status);
  }
});
const mes_order_status = status;

/**
 * 到货通知搜索表单的字段配置
 */
export function useArrivalScheam() {
  return [
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入通知单编号',
        allowClear: true,
        class: 'mr-5',
      },
      fieldName: 'vendorCode',
      label: '通知单编号',
      labelClass: ' text-[#606266] font-bold',
    },
    {
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入通知单名称',
      },
      fieldName: 'noticeName',
      label: '通知单名称',
      labelClass: ' text-[#606266] font-bold',
    },
    {
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入采购订单编号',
      },
      fieldName: 'poCode',
      label: '采购订单编号',
      labelClass: '  text-[#606266] font-bold',
    },
    {
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入供应商名称',
      },
      fieldName: 'vendorName',
      label: '供应商名称',
      labelClass: '  text-[#606266] font-bold',
    },
    {
      component: 'DatePicker',
      componentProps: {
        placeholder: '请选择到货日期',
      },
      fieldName: 'arrivalDate',
      label: '到货日期',
      labelClass: '  text-[#606266] font-bold',
    },
    {
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: mes_order_status,
        placeholder: '请选择字典生成',
      },
      fieldName: 'status',
      label: '单据状态',
      labelClass: '  text-[#606266] font-bold',
    },
  ];
}

/**
 * 获取采购入库中到货通知表格列配置
 * @param _onActionClick 表格操作按钮点击事件
 */
export function useArrivalColumns(
  _onActionClick?: OnActionClickFn<ArrivalNotice.Row>,
): VxeTableGridOptions<ArrivalNotice.Row>['columns'] {
  return [
    {
      field: 'checkbox',
      fixed: 'left',
      type: 'radio',
      width: 50,
    },
    {
      field: 'noticeCode',
      fixed: 'left',
      title: '通知单编号',
      width: 150,
    },
    {
      field: 'noticeName',
      fixed: 'left',
      title: '通知单名称',
    },
    {
      field: 'poCode',
      title: '采购订单号',
    },
    {
      field: 'vendorName',
      title: '供应商名称',
    },
    {
      field: 'contact',
      title: '联系人',
    },

    {
      field: 'tel',
      title: '联系方式',
    },
    {
      field: 'arrivalDate',
      title: '到货日期',
    },
  ];
}

/**
 * 获取采购入库中物料信息表格列配置
 * @param status  单据状态
 * @param onView  查看按钮事件
 * @param _onActionClick 表格操作按钮点击事件
 */
export function useItemRecptColumns(
  status: string,
  onView: (param: ItemRecptLine.Row) => void,
  _onActionClick?: OnActionClickFn<ItemRecptLine.Row>,
): VxeTableGridOptions<ItemRecptLine.Row>['columns'] {
  return [
    {
      field: 'recptId',
      fixed: 'left',
      type: status === 'PREPARE' ? 'checkbox' : 'expand',
      slots: { content: 'expand_content' },
      width: 50,
    },
    {
      field: 'itemCode',
      fixed: 'left',
      title: '物料编码',
      width: 150,
      cellRender:
        status === 'PREPARE'
          ? { name: 'CellButtonInfo', props: { onClick: onView } }
          : undefined,
    },
    {
      field: 'itemName',
      fixed: 'left',
      title: '物料名称',
    },
    {
      field: 'specification',
      title: '规格型号',
    },
    {
      field: 'unitName',
      title: '单位名称',
    },
    {
      field: 'quantityRecived',
      title: '入库数量',
    },
    {
      field: 'batchCode',
      title: '批次号',
    },
    {
      field: 'operation',
      title: '操作',
      width: 200,
      fixed: 'right',
      align: 'center',
      slots: { default: 'action' },
    },
  ];
}

/**
 * 获取物料入库单编辑表单数据
 *
 */
export function useItemRecptLineScheam() {
  return [
    {
      component: 'Input',
      fieldName: 'lineId',
      dependencies: {
        triggerFields: ['lineId'],
        show: false,
      },
    },
    {
      component: 'Input',
      fieldName: 'itemId',
      dependencies: {
        triggerFields: ['itemId'],
        show: false,
      },
    },
    {
      component: 'Input',
      fieldName: 'recptId',
      dependencies: {
        triggerFields: ['recptId'],
        show: false,
      },
    },
    {
      component: '',
      fieldName: 'itemCode',
      rules: 'required',
      label: '物料编码',
      labelWidth: 70,
      formItemClass: 'col-span-5',
      componentProps: {
        class: 'w-full',
      },
    },
    {
      component: '',
      rules: 'required',
      formItemClass: 'col-span-4',
      componentProps: {
        placeholder: '请输入物料名称',
      },
      labelWidth: 70,
      fieldName: 'itemName',
      label: '物料名称',
    },
    {
      component: '',
      fieldName: 'unitName',
      labelWidth: 50,
      formItemClass: 'col-span-3',
      label: '单位',
      componentProps: {
        placeholder: '',
        class: 'w-full',
        readonly: true,
      },
    },
    {
      component: 'ElInputNumber',
      fieldName: 'quantityRecived',
      formItemClass: 'col-span-4',
      defaultValue: 1,
      labelWidth: 70,
      label: '入库数量',
    },
    {
      component: 'DatePicker',
      fieldName: 'produceDate',
      formItemClass: 'col-span-4',
      labelWidth: 70,
      label: '生产日期',
    },

    {
      component: 'DatePicker',
      fieldName: 'expireDate',
      formItemClass: 'col-span-4',
      labelWidth: 70,
      label: '有效日期',
    },
    {
      component: 'Input',
      fieldName: 'lotNumber',
      formItemClass: 'col-span-4',
      labelWidth: 70,
      label: '生产批号',
    },
    {
      component: 'Input',
      fieldName: 'batchCode',
      formItemClass: 'col-span-4',
      labelWidth: 70,
      componentProps: {
        class: 'w-full',
        readonly: true,
      },
      label: '批次号',
    },
    {
      component: 'Textarea',
      formItemClass: 'col-span-12',
      componentProps: {
        placeholder: '请输入内容',
        maxLength: 50,
        rows: 3,
        showCount: false,
      },
      fieldName: 'remark',
      label: '备注',
      labelWidth: 70,
    },
  ];
}
